
import { parentPort, workerData } from 'worker_threads';
import { DataHandler, Period } from '../../trader';
import { KLineDTO } from '../kline-history/KLineHistory.entity';

import { keepDecimalFixed, transformToCSV } from '../../utils';
import { Quant } from '../../lib/quant';
import { Vector } from '../../trader/indicators/vector';
import { MA } from '../../lib/quant/indicator/ma';

let history: KLineDTO[] = [];
if (parentPort) {
  history = workerData.history;
  // 在主进程或主线程中
  parentPort.on('message', msg => {
    parentPort.postMessage(toVector(msg));
  });
} else {
  // 在 fork 或 worker 中
  process.on("message", msg => {
    if (msg.type && msg.type === 'workerData') {
      history = msg.workerData.history
      return;
    }
    // 将结果发送给主线程
    process.send(toVector(msg));
  });
}

export interface  VectorData {
  vector: number[]; 
  timestamp: number;
  close_ma_rate: number;
  min: number;
  max: number;
}
export function toVector ({period}: {period: Period}) {
  if (period === '4h') {
    return toVector4h();
  } else if (period === '1d') {
    return toVector1d();
  } 
}
export function toVector4h() {

  const vectors: VectorData[] = [];
  // 指标
  const quant = new Quant();
  quant.addIndicator('vector', new Vector({ period: 90 }));
  quant.addIndicator('ma120', new MA({ period: 120, key: 'close' }));
  const dataHander = new DataHandler('mock');
  
  dataHander.on('interval:4h', (data) => {
    quant.addData(data);
  });
  quant.addStrategy('mix-indicator', (indicators, bar) => {
    const vector = (indicators.get('vector') as Vector);
    const ma120 = (indicators.get('ma120') as MA);
    vectors.push({
      // symbol,
      vector: vector.getValue().vector,
      min: vector.getValue().min,
      max: vector.getValue().max,
      timestamp: bar.timestamp,
      close_ma_rate: keepDecimalFixed((bar.close - ma120.getValue()) / ma120.getValue(), 4)
    });
    return undefined
  });

  history.forEach(item => {
    dataHander.handleHistory(item)
  });
  
  return transformToCSV(vectors)
}

export function toVector1d() {

  const vectors: VectorData[] = [];
  // 指标
  const quant = new Quant();
  quant.addIndicator('vector', new Vector({ period: 30 }));
  quant.addIndicator('ma60', new MA({ period: 60, key: 'close' }));
  const dataHander = new DataHandler('mock');

  dataHander.on('interval:1d', (data) => {
    quant.addData(data);
  });

  quant.addStrategy('mix-indicator', (indicators, bar) => {
    const vector = (indicators.get('vector') as Vector);
    const ma60 = (indicators.get('ma60') as MA);
    vectors.push({
      // symbol,
      vector: vector.getValue().vector,
      min: vector.getValue().min,
      max: vector.getValue().max,
      timestamp: bar.timestamp,
      close_ma_rate: keepDecimalFixed((bar.close - ma60.getValue()) / ma60.getValue(), 4)
    });
    return undefined
  });

  history.forEach(item => {
    dataHander.handleHistory(item)
  });
  
  return transformToCSV(vectors)
}